package com.wzg.jetpack_blackbox.login.source;

import com.blankj.utilcode.util.LogUtils;
import com.blankj.utilcode.util.ThreadUtils;
import com.blankj.utilcode.util.ToastUtils;
import com.wzg.jetpack_blackbox.R;
import com.wzg.jetpack_blackbox.login.room.IUserDao;
import com.wzg.jetpack_blackbox.login.room.TestBean2;
import com.wzg.jetpack_blackbox.login.room.User;
import com.wzg.jetpack_blackbox.login.room.UserWork;
import com.wzg.jetpack_blackbox.room.AppDataBase;

import java.util.ArrayList;


public class LoginLocalDataSource implements ILoginLocalDataSource {

    private final IUserDao userDao;

    public LoginLocalDataSource() {
        userDao = AppDataBase.getInstance().getUserDao();
    }

    @Override
    public void save2disk(String name, String pwd) {

        User queryUser = userDao.queryByUserId(pwd + name);

        if (queryUser != null && name.equals(queryUser.getName()) && pwd.equals(queryUser.getPwd())) {
            ToastUtils.showLong("用户存在，一致登陆成功");
            return;
        }


        User user = new User();

        UserWork theUserWork = new UserWork();
        theUserWork.setWorkName("保安");
        theUserWork.setWorkStartTime("2022-10-00");
        user.setUserWork(theUserWork);

        TestBean2 testBean2 = new TestBean2();
        testBean2.setA("e");
        testBean2.setB("16");
        user.setTestBean2(testBean2);

        UserWork userWork2 = new UserWork();
        userWork2.setWorkStartTime("1998-08-01");
        userWork2.setWorkName("son");

        ArrayList<UserWork> userWorks = new ArrayList<>();

        userWorks.add(userWork2);
        userWorks.add(theUserWork);
        user.setList(userWorks);

        user.setUserId(pwd + name);

        user.setName(name);
        user.setPwd(pwd);
        user.setLoginTime(String.valueOf(System.currentTimeMillis()));
        user.setA("000000A");

        userDao.insertUser(user);

        ThreadUtils.getMainHandler().post(new Runnable() {
            @Override
            public void run() {
                LogUtils.iTag("room", "insertUser***" + name + "\t");
                ToastUtils.showLong("insert success,注册成功");
            }
        });


    }


}
